const { query } = require("./mysql");
const {
	YD_datetimeToString,
	YD_whereSql,
	YD_setSql,
} = require("lib/utils/common");

/** @name 查询用户---username **/
exports.yd_find = (username) => {
	const sql = `select
			*,
			${YD_datetimeToString("lastTime")},
			${YD_datetimeToString("createTime")},
			${YD_datetimeToString("updateTime")}
		from ch_user where username='${username}'
	`;
	return query(sql);
};

/** @name 查询用户---uuid **/
exports.yd_query = (uuid) => {
	const sql = `select
			*,
			${YD_datetimeToString("lastTime")},
			${YD_datetimeToString("createTime")},
			${YD_datetimeToString("updateTime")}
		from ch_user where uuid='${uuid}'
	`;
	return query(sql);
};

/** @name 查询用户---openId **/
exports.yd_findOpenId = (openId) => {
	const sql = `select
			*,
			${YD_datetimeToString("lastTime")},
			${YD_datetimeToString("createTime")},
			${YD_datetimeToString("updateTime")}
		from ch_user where openId='${openId}'
	`;
	return query(sql);
};

/** @name 更新登录时间 **/
exports.yd_updateLastTime = (options) => {
	const sql = `update ch_user set lastTime='${options.lastTime}',
            updateTime='${options.updateTime}'
            where uuid='${options.uuid}'
            `;
	return query(sql);
};

/** @name 用户列表 **/
exports.yd_list = (options) => {
	const sql = `select
						uuid, openId, username, nickname, realname, avatar, gender,
						phone, email, role, status, note,
						${YD_datetimeToString("lastTime")},
						${YD_datetimeToString("createTime")},
						${YD_datetimeToString("updateTime")}
						from ch_user
						${YD_whereSql([
							{ field: "username", type: "模糊", value: options.username },
							{ field: "gender", type: "全等", value: options.gender },
							{ field: "nickname", type: "模糊", value: options.nickname },
							{ field: "realname", type: "模糊", value: options.realname },
							{ field: "phone", type: "模糊", value: options.phone },
							{ field: "email", type: "模糊", value: options.email },
							{ field: "role", type: "全等", value: options.role },
							{ field: "status", type: "全等", value: options.status },
							{
								field: "createTime",
								type: "日期区间",
								value: options.createTime,
							},
						])}
						order by createTime desc
						limit ${(options.pageNum - 1) * options.pageSize},${options.pageSize}
            `;
	return query(sql);
};

/** @name 用户总量 **/
exports.yd_count = (options) => {
	const sql = `select count(*) as count
						from ch_user
						${YD_whereSql([
							{ field: "username", type: "模糊", value: options.username },
							{ field: "gender", type: "全等", value: options.gender },
							{ field: "nickname", type: "模糊", value: options.nickname },
							{ field: "realname", type: "模糊", value: options.realname },
							{ field: "phone", type: "模糊", value: options.phone },
							{ field: "email", type: "模糊", value: options.email },
							{ field: "role", type: "全等", value: options.role },
							{ field: "status", type: "全等", value: options.status },
							{
								field: "createTime",
								type: "日期区间",
								value: options.createTime,
							},
						])}
            `;
	return query(sql);
};

/** @name 添加用户 **/
exports.yd_add = (options) => {
	const sql = `insert into ch_user set ${YD_setSql(options)}`;
	return query(sql);
};

/** @name 编辑用户 **/
exports.yd_update = (options, condition) => {
	const sql = `update ch_user set ${YD_setSql(options)} where uuid='${
		condition.uuid
	}'`;
	return query(sql);
};

/** @name 删除用户 **/
exports.yd_delete = (uuid) => {
	const sql = `delete from ch_user where uuid='${uuid}'`;
	return query(sql);
};

/** @name 简易用户列表 **/
exports.yd_easyList = () => {
	const sql = `select
						uuid, nickname, realname, avatar, gender, phone
						from ch_user
						where status=1
            `;
	return query(sql);
};
